package edu.gxmzu.task.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import edu.gxmzu.task.entity.UserInfo;
import edu.gxmzu.task.entity.UserManageInfo;
import edu.gxmzu.task.models.UserInfoModel;
import edu.gxmzu.task.models.UserModel;
import org.apache.ibatis.annotations.*;

import java.util.List;


@Mapper
public interface UserInfoMapper extends BaseMapper<UserInfo> {

    @Select("SELECT a.user_id, a.username, a.email, r.name as role_name, u.name, u.gender, u.department_name, u.img, u.birthday " +
            "FROM accounts AS a, user_info AS u, roles as r " +
            "WHERE a.user_id = u.user_id AND u.role_id = r.id AND a.user_id = #{userId}")
    UserModel selectUserInfoById(@Param("userId") String userId);

    @Select("SELECT r.name " +
            "FROM user_info AS u, roles AS r " +
            "WHERE u.role_id = r.id AND u.user_id = #{userId}")
    List<String> selectRolesByUserId(@Param("userId") String userId);

    /**
     * author: lrf
     * time: 2024-11-29
     * version: V1.1
     * content: 查询用户管理页面数据
     */
    /**
     * author: lrf
     * time: 2024-11-29
     * version: V1.1
     * content: 查询用户管理页面数据
     */
    @Select("SELECT \n" +
            "    a.user_id,\n" +
            "    u.name,\n" +
            "    a.username, \n" +
            "    u.role_id, \n" +
            "    u.gender, \n" +
            "    u.birthday, \n" +
            "    a.email,\n" +
            "    GROUP_CONCAT(att.name SEPARATOR ', ') AS attachment_names\n" +
            "FROM \n" +
            "    accounts AS a\n" +
            "LEFT JOIN \n" +
            "    user_info AS u ON a.user_id = u.user_id\n" +
            "LEFT JOIN\n" +
            "    works AS w ON a.user_id = w.user_id\n" +
            "LEFT JOIN\n" +
            "    attachments AS att ON att.id = w.attachments_id\n" +
            "WHERE \n" +
            "    u.role_id = 0\n" +
            "    and u.name LIKE CONCAT('%', COALESCE(#{name}, ''), '%')\n" +
            "    and a.deletion_state=0\n"+
            "GROUP BY \n" +
            "    a.user_id, u.name, a.username, u.role_id, u.gender, u.birthday, a.email")
    List<UserManageInfo> getUserManageInfo(@Param("name") String name);

    @Select("select username from accounts where user_id = #{userId}")
    String getUserName(String userId);

    /**
     * author: lrf
     * time: 2024-11-29
     * version : V1
     * content: 密码重置
     */
    @Update("UPDATE accounts SET password = #{encodePassword} WHERE user_id = #{userId}")
    int updatePassword(@Param("userId") String userId, @Param("encodePassword") String encodePassword);

    /**
     * author: lrf
     * time: 2024-11-29
     * version : V1
     * content: 编辑用户信息
     */
    @Update("UPDATE accounts a " +
            "JOIN user_info u ON a.user_id = u.user_id\n" +
            "JOIN works w ON a.user_id = w.user_id\n" +
            "LEFT JOIN attachments att ON att.id = w.attachments_id\n" +
            "SET\n" +
            "    a.username = #{username},\n" +

            "    a.email = #{email},\n" +
            "    u.name = #{name},\n" +
            "    u.role_id = #{roleId},\n" +
            "    u.gender = #{gender},\n" +
            "    u.birthday = #{birthday},\n" +
            "    w.attachments_id = #{attachmentsId}\n" +
            "WHERE" +
            "    a.user_id = #{userId}")
    int editUserInfo(UserManageInfo userManageInfo);

    @Delete("<script>" +
            "DELETE FROM user_info WHERE user_id IN " +
            "<foreach collection='list' item='userId' open='(' close=')' separator=','>" +
            "#{userId}" +
            "</foreach>" +
            "</script>")
    int deleteUserInfoById(@Param("list") List<String> userIds);

    /**
     * @Author hh
     * @Description //查询个人信息
     * @Date 13:08 2024/12/5
     * @Param
     * @return
     **/
    @Select("SELECT a.user_id,u.name,u.birthday,u.role_id,a.email,a.username,u.gender,u.img\n" +
            "FROM accounts AS a,user_info AS u "+
            " WHERE a.user_id =u.user_id AND a.user_id =#{userId}" +
            "")
    UserInfoModel UserInfo(@Param("userId") String userId);



   /* @Update("START TRANSACTION;\n" +
            "UPDATE accounts SET email = #{email}, username = #{username} WHERE user_id = #{userId};\n" +
            "UPDATE user_info SET role_id = #{roleId}, name = #{name}, gender = #{gender}, img = #{img} WHERE user_id = #{userId};\n" +
            "COMMIT;")
    int UpdateUserInfoById(UserInfoModel userInfoModel);*/

    @Update("UPDATE accounts\n" +
            "SET email = COALESCE(#{email}, email),\n" +
            "    username = COALESCE(#{username}, username),\n" +
            "    password = COALESCE(#{password}, password)\n" +
            "WHERE user_id = #{userId};")
    int updateAccounts(UserInfoModel userInfoModel);

    @Update("UPDATE user_info SET role_id = #{roleId}, name = #{name}, gender = #{gender},birthday = #{birthday} ,img = #{img} WHERE user_id = #{userId};")
    int updateUserInfo(UserInfoModel userInfoModel);


}
